using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using Voodoo.GameOps;
using Voodoo.Pattern;

public class Sell : MonoBehaviour
{
	public bool IsPlayer = true;

	[SerializeField]
	private Rigidbody2D _quater;

	[SerializeField]
	private Rigidbody2D _half;

	[SerializeField]
	private Rigidbody2D _one;

	[SerializeField]
	private Rigidbody2D _three;

	[SerializeField]
	private Rigidbody2D _five;

	[SerializeField]
	private Rigidbody2D _twenty;

	[SerializeField]
	private Transform _spawnPoint;

	[SerializeField]
	private GameObject _XText;

	[SerializeField]
	private GameObject _MultiplierObject;

	[SerializeField]
	private GameObject _MultiplierAnim;

	[SerializeField]
	private TextMeshPro _MultiplierText;

	[Header("Multiplier RV Button")]
	[SerializeField]
	private GameObject _MultiplierRV;

	[SerializeField]
	private GameObject _VideoImage;

	[SerializeField]
	private TextMeshProUGUI _MultiplierRVText;

	[SerializeField]
	private GameObject _MultiplierFX;

	private float[] values = new float[6] { 0.25f, 0.5f, 1f, 3f, 5f, 20f };

	private List<Rigidbody2D> _quaterL = new List<Rigidbody2D>();

	private List<Rigidbody2D> _halfL = new List<Rigidbody2D>();

	private List<Rigidbody2D> _oneL = new List<Rigidbody2D>();

	private List<Rigidbody2D> _threeL = new List<Rigidbody2D>();

	private List<Rigidbody2D> _fiveL = new List<Rigidbody2D>();

	private List<Rigidbody2D> _twentyL = new List<Rigidbody2D>();

	public TransitionMachine _transitionMachine;

	private Money Money;

	public float deltaMoney;

	private float X = 1f;

	public Item MultiplierUpgrade;

	private int RVmultiplier = 2;

	private GameObject CoinParent;

	private float chosenMultiplier = 1f;

	private float lastAnim;

	private bool FirstTimeRVBoost
	{
		get
		{
			return NintendoPlayerPrefs.GetInt("Sell_First_Time_RV", 0) == 0;
		}
		set
		{
			NintendoPlayerPrefs.SetInt("Sell_First_Time_RV", (!value) ? 1 : 0);
		}
	}

	private void Start()
	{
		_MultiplierRV.SetActive(value: false);
		CoinParent = new GameObject();
		CoinParent.name = "Coins";
		for (int i = 0; i < 10; i++)
		{
			_quaterL.Add(UnityEngine.Object.Instantiate(_quater, CoinParent.transform));
			_halfL.Add(UnityEngine.Object.Instantiate(_half, CoinParent.transform));
			_oneL.Add(UnityEngine.Object.Instantiate(_one, CoinParent.transform));
			_threeL.Add(UnityEngine.Object.Instantiate(_three, CoinParent.transform));
			_fiveL.Add(UnityEngine.Object.Instantiate(_five, CoinParent.transform));
			_twentyL.Add(UnityEngine.Object.Instantiate(_twenty, CoinParent.transform));
		}
		GameManager instance = SingletonMB<GameManager>.Instance;
		instance.OnGameStart = (Action)Delegate.Combine(instance.OnGameStart, new Action(OnGameStart));
		GameManager instance2 = SingletonMB<GameManager>.Instance;
		instance2.OnGameEnd = (Action)Delegate.Combine(instance2.OnGameEnd, new Action(OnGameEnd));
		Money = UnityEngine.Object.FindObjectOfType<Money>();
		_MultiplierObject.SetActive(value: false);
		if (SingletonMB<GameManager>.Instance.IsDuelMode)
		{
			if (IsPlayer)
			{
				DuelModeManager instance3 = SingletonMB<DuelModeManager>.Instance;
				instance3.OnUpgradedPlayer = (Action)Delegate.Combine(instance3.OnUpgradedPlayer, new Action(OnGameStart));
			}
			else
			{
				DuelModeManager instance4 = SingletonMB<DuelModeManager>.Instance;
				instance4.OnUpgradedOpponent = (Action)Delegate.Combine(instance4.OnUpgradedOpponent, new Action(OnGameStart));
			}
		}
		else if (Preloader.ShopConfig.UpgradeShop)
		{
			InventoryManager instance5 = InventoryManager.Instance;
			instance5.OnItemPurchased = (Action)Delegate.Combine(instance5.OnItemPurchased, new Action(OnGameStart));
		}
		OnGameStart();
	}

	private void OnGameEnd()
	{
		chosenMultiplier = 1f;
		if (InventoryManager.Instance.GetInventoryCount(MultiplierUpgrade) > 0)
		{
			chosenMultiplier *= 1f + (float)InventoryManager.Instance.GetInventoryCount(MultiplierUpgrade) * 0.1f;
		}
		if (chosenMultiplier > 1f)
		{
			_MultiplierObject.SetActive(value: true);
			_MultiplierText.text = "X" + chosenMultiplier;
			StartCoroutine(ScaleAnim());
		}
		else
		{
			_MultiplierObject.SetActive(value: false);
		}
	}

	private void OnDestroy()
	{
		if (InventoryManager.Instance != null)
		{
			InventoryManager instance = InventoryManager.Instance;
			instance.OnItemPurchased = (Action)Delegate.Remove(instance.OnItemPurchased, new Action(OnGameStart));
		}
		if (SingletonMB<GameManager>.Instance != null)
		{
			GameManager instance2 = SingletonMB<GameManager>.Instance;
			instance2.OnGameStart = (Action)Delegate.Remove(instance2.OnGameStart, new Action(OnGameStart));
			GameManager instance3 = SingletonMB<GameManager>.Instance;
			instance3.OnGameEnd = (Action)Delegate.Remove(instance3.OnGameEnd, new Action(OnGameEnd));
		}
	}

	public void OnGameStart()
	{
		chosenMultiplier = 1f;
		if (Preloader.ABTestConfig.RVStartBoost && SingletonMB<GameManager>.Instance.BoostLevel)
		{
			chosenMultiplier *= 3f;
		}
		if (SingletonMB<DuelModeManager>.Instance != null)
		{
			if (IsPlayer)
			{
				chosenMultiplier *= SingletonMB<DuelModeManager>.Instance.PlayerUpgrades.Multiplier;
			}
			else
			{
				chosenMultiplier *= SingletonMB<DuelModeManager>.Instance.OpponentUpgrades.Multiplier;
			}
		}
		else if (InventoryManager.Instance.GetInventoryCount(MultiplierUpgrade) > 0)
		{
			chosenMultiplier *= 1f + (float)InventoryManager.Instance.GetInventoryCount(MultiplierUpgrade) * 0.1f;
		}
		if (chosenMultiplier > 1f)
		{
			_MultiplierObject.SetActive(value: true);
			_MultiplierText.text = "X" + chosenMultiplier;
			StartCoroutine(ScaleAnim());
		}
		else
		{
			_MultiplierObject.SetActive(value: false);
		}
		if (_transitionMachine != null)
		{
			_transitionMachine.multiplier = (int)chosenMultiplier;
		}
		if (Preloader.ABTestConfig.MultiplierButton)
		{
			StartCoroutine(WaitAndActivateRVButton());
		}
		_MultiplierFX.SetActive(value: false);
	}

	private IEnumerator WaitAndActivateRVButton()
	{
		while (SingletonMB<Progress>.Instance.Pr() < 20)
		{
			yield return null;
		}
		_VideoImage.SetActive(!FirstTimeRVBoost);
		_MultiplierRV.SetActive(value: true);
		if (chosenMultiplier <= 1f)
		{
			RVmultiplier = 3;
		}
		else
		{
			RVmultiplier = 2;
		}
		while (true)
		{
			_MultiplierRVText.text = "+" + Mathf.CeilToInt((float)RVmultiplier * deltaMoney);
			yield return null;
		}
	}

	public void AugmentMultiplier(int augment)
	{
		chosenMultiplier *= augment;
		_MultiplierObject.SetActive(value: true);
		_MultiplierText.text = "X" + chosenMultiplier;
		StopCoroutine(ScaleAnim());
		StartCoroutine(ScaleAnim());
	}

	public void OnClickRV()
	{
		_MultiplierRV.SetActive(value: false);
		if (FirstTimeRVBoost)
		{
			InventoryManager.Instance.AddMoney(Mathf.CeilToInt((float)RVmultiplier * deltaMoney));
			_MultiplierFX.SetActive(value: false);
			_MultiplierFX.SetActive(value: true);
			SingletonMB<GameManager>.Instance.GameplayHaptic.PlayHaptic(HapticType.Big);
			FirstTimeRVBoost = false;
		}
		else
		{
			SingletonMB<GameManager>.Instance.GameplayHaptic.PlayHaptic(HapticType.Big);
			InventoryManager.Instance.AddMoney(Mathf.CeilToInt((float)RVmultiplier * deltaMoney));
			_MultiplierFX.SetActive(value: false);
			_MultiplierFX.SetActive(value: true);
		}
	}

	private void OnTriggerEnter(Collider other)
	{
		if (other.tag == "Player")
		{
			other.gameObject.SetActive(value: false);
			if (UnityEngine.Random.Range(3, 4) == 3)
			{
				EarnCoin(UnityEngine.Random.Range(1, 6));
			}
			if (Preloader.RailDigging.IsEnabled)
			{
				other.transform.localPosition += new Vector3(0f, -2f, 1f);
			}
		}
		else if (other.tag == "GemCollected")
		{
			other.gameObject.SetActive(value: false);
			other.transform.Find("Gem(Clone)").gameObject.SetActive(value: false);
			other.transform.Find("Gem(Clone)").parent = null;
			EarnCoin(6);
		}
	}

	private IEnumerator ScaleAnim()
	{
		while (true)
		{
			_MultiplierAnim.transform.localScale = Vector3.Lerp(_MultiplierAnim.transform.localScale, Vector3.one, Time.deltaTime);
			yield return null;
		}
	}

	private void EarnCoin(int value)
	{
		List<Rigidbody2D> list = new List<Rigidbody2D>();
		if (value == 1)
		{
			list = _quaterL;
		}
		if (value == 2)
		{
			list = _halfL;
		}
		if (value == 3)
		{
			list = _oneL;
		}
		if (value == 4)
		{
			list = _threeL;
		}
		if (value == 5)
		{
			list = _fiveL;
		}
		if (value == 6)
		{
			list = _twentyL;
		}
		float num = chosenMultiplier;
		if (value == 6)
		{
			if (Preloader.WallGemsConfig.GemsValueMultiplier == 0f)
			{
				NintendoPlayerPrefs.SetInt("Gems", NintendoPlayerPrefs.GetInt("Gems", 0) + 1);
				SingletonMB<GemsCounter>.Instance.UpdateValue();
			}
			else if (Preloader.CityBuilderConfig.IsEnabled)
			{
				NintendoPlayerPrefs.SetInt("Gems", NintendoPlayerPrefs.GetInt("Gems", 0) + 1);
				SingletonMB<GemsCounter>.Instance.UpdateValue();
				NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) + Preloader.WallGemsConfig.GemsValueMultiplier);
			}
			else
			{
				NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) + Preloader.WallGemsConfig.GemsValueMultiplier);
			}
		}
		else
		{
			float num2 = values[value - 1] * X * num;
			if (SingletonMB<GameManager>.Instance.IsDuelMode)
			{
				if (IsPlayer)
				{
					SingletonMB<DuelModeManager>.Instance.PlayerMoney += num2;
				}
				else
				{
					SingletonMB<DuelModeManager>.Instance.OpponentMoney += num2;
				}
			}
			SingletonMB<SkinMissionController>.Instance.ProgressMission(SkinMissionType.Money, (int)num2);
			deltaMoney += num2;
			if (_transitionMachine != null)
			{
				_transitionMachine.CollectedCoin(num2);
			}
		}
		if (num > 1f)
		{
			_MultiplierAnim.transform.localScale = new Vector3(2f, 2f, 1f);
		}
		for (int i = 0; i < list.Count; i++)
		{
			if (!list[i].simulated)
			{
				Rigidbody2D rigidbody2D = list[i];
				if (list == _twentyL)
				{
					list[i].transform.GetChild(0).GetComponent<TextMeshPro>().text = ((Preloader.WallGemsConfig.GemsValueMultiplier == 0f || Preloader.CityBuilderConfig.IsEnabled) ? ((int)Preloader.WallGemsConfig.GemsValueMultiplier).ToString() : "");
				}
				rigidbody2D.simulated = true;
				rigidbody2D.position = _spawnPoint.position + new Vector3(UnityEngine.Random.Range(-0.2f, 0.2f), 0f, 0f);
				StartCoroutine(KillCoin(rigidbody2D));
				break;
			}
		}
	}

	private IEnumerator KillCoin(Rigidbody2D coin)
	{
		yield return new WaitForSeconds(2f);
		coin.transform.position = new Vector3(-30f, 0f, 0f);
		coin.simulated = false;
	}

	public void SetMultyplyer(float value)
	{
		X = value;
		_XText.SetActive(value: true);
		_XText.transform.GetChild(0).GetComponent<TextMeshPro>().text = "x" + value;
	}

	public void ResetMultyplyer()
	{
		X = 1f;
		_XText.SetActive(value: false);
		deltaMoney = 0f;
	}
}
